package com.shanghaionstar.mro.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;

import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.DealerBrandType;

public class MrOBrand extends RelativeLayout {
    private View view;
    private CustomImageView iv_bigbody_normal;
    private CustomImageView iv_bigbody_listen;
    private CustomImageView iv_bigbody_loading;
    private CustomImageView iv_bighead;
    private CustomImageView iv_bigeye;
    private CustomImageView iv_bigmouth;
    private CustomImageView iv_listen_mouth;
    private CustomImageView iv_loading_mouth;

    private Animation loading_bodyRotateAnimation;
    private Animation loading_eyeRotateAnimation;

    private Animation listen_bodyRotateAnimation;
    private Animation listen_eyeRotateAnimation;

    private DealerBrandType brandType = DealerBrandType.DEFAULT;

    public MrOBrand(Context context) {
        super(context);
        initView();

    }

    public MrOBrand(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        view = inflate(getContext(), R.layout.mro_brand, this);
        iv_bigbody_normal = (CustomImageView) view.findViewById(R.id.iv_bigbody_normal);
        iv_bigbody_listen = (CustomImageView) view.findViewById(R.id.iv_bigbody_listen);
        iv_bigbody_loading = (CustomImageView) view.findViewById(R.id.iv_bigbody_loading);
        iv_bighead = (CustomImageView) view.findViewById(R.id.iv_bighead);
        iv_bigeye = (CustomImageView) view.findViewById(R.id.iv_bigeye);
        iv_bigmouth = (CustomImageView) view.findViewById(R.id.iv_bigmouth);
        iv_listen_mouth = (CustomImageView) view.findViewById(R.id.iv_listen_mouth);
        iv_loading_mouth = (CustomImageView) view.findViewById(R.id.iv_loading_mouth);
        normal();
    }

    public void changeBrand(DealerBrandType dealerBrandType) {
        if (brandType == dealerBrandType)
            return;
        brandType = dealerBrandType;
        switch (dealerBrandType) {
            case DEFAULT:
                iv_bigbody_normal.setImageResource(R.drawable.mro_body_normal);
                iv_bigbody_listen.setImageResource(R.drawable.mro_body_listen);
                iv_bigbody_loading.setImageResource(R.drawable.mro_body_loading);
                iv_bighead.setImageResource(R.drawable.mro_head);
                break;
            case BUICK:
                iv_bigbody_normal.setImageResource(R.drawable.mro_body_normal_buick);
                iv_bigbody_listen.setImageResource(R.drawable.mro_body_listen_buick);
                iv_bigbody_loading.setImageResource(R.drawable.mro_body_loading_buick);
                iv_bighead.setImageResource(R.drawable.mro_head);
                break;
            case CHEVROLET:
                iv_bigbody_normal.setImageResource(R.drawable.mro_body_normal_chevrolet);
                iv_bigbody_listen.setImageResource(R.drawable.mro_body_listen_chevrolet);
                iv_bigbody_loading.setImageResource(R.drawable.mro_body_loading_chevrolet);
                iv_bighead.setImageResource(R.drawable.mro_head);
                break;
            case CADILLAC:
                iv_bigbody_normal.setImageResource(R.drawable.mro_body_normal_cadillac);
                iv_bigbody_listen.setImageResource(R.drawable.mro_body_listen_cadillac);
                iv_bigbody_loading.setImageResource(R.drawable.mro_body_loading_cadillac);
                iv_bighead.setImageResource(R.drawable.mro_head);
                break;
            case WTK:
                iv_bigbody_normal.setImageResource(R.drawable.mro_body_normal);
                iv_bigbody_listen.setImageResource(R.drawable.mro_body_listen);
                iv_bigbody_loading.setImageResource(R.drawable.mro_body_loading);
                iv_bighead.setImageResource(R.drawable.mro_head_wtk);
                break;
        }
    }

    public void normal() {
        iv_bigbody_normal.setVisibility(View.VISIBLE);
        iv_bigbody_listen.setVisibility(View.GONE);
        iv_bigbody_loading.setVisibility(View.GONE);

        iv_bigmouth.setVisibility(View.VISIBLE);
        iv_listen_mouth.setVisibility(View.GONE);
        iv_loading_mouth.setVisibility(View.GONE);

    }

    public void listen() {
        iv_bigbody_normal.setVisibility(View.GONE);
        iv_bigbody_listen.setVisibility(View.VISIBLE);
        iv_bigbody_loading.setVisibility(View.GONE);

        iv_bigmouth.setVisibility(View.GONE);
        iv_listen_mouth.setVisibility(View.VISIBLE);
        iv_loading_mouth.setVisibility(View.GONE);

        listen_bodyRotateAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.mro_head);
        listen_eyeRotateAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.mro_eye);
        LinearInterpolator lin = new LinearInterpolator();
        listen_bodyRotateAnimation.setInterpolator(lin);
        listen_eyeRotateAnimation.setInterpolator(lin);
        listen_bodyRotateAnimation.setFillAfter(true);
        listen_eyeRotateAnimation.setFillAfter(true);

        iv_bighead.startAnimation(listen_bodyRotateAnimation);
        iv_bigeye.startAnimation(listen_eyeRotateAnimation);
    }

    public void loading() {
        if (listen_bodyRotateAnimation != null) {

            listen_bodyRotateAnimation.setFillAfter(false);
        }
        if (listen_eyeRotateAnimation != null) {

            listen_eyeRotateAnimation.setFillAfter(false);
        }
        iv_bigbody_normal.setVisibility(View.GONE);
        iv_bigbody_listen.setVisibility(View.GONE);
        iv_bigbody_loading.setVisibility(View.VISIBLE);

        iv_bigmouth.setVisibility(View.GONE);
        iv_listen_mouth.setVisibility(View.GONE);
        iv_loading_mouth.setVisibility(View.VISIBLE);

        loading_bodyRotateAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.mro_head2);
        loading_eyeRotateAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.mro_eye2);
        LinearInterpolator lin = new LinearInterpolator();
        loading_bodyRotateAnimation.setInterpolator(lin);
        loading_eyeRotateAnimation.setInterpolator(lin);
        loading_bodyRotateAnimation.setFillAfter(true);
        loading_eyeRotateAnimation.setFillAfter(true);
        iv_bighead.startAnimation(loading_bodyRotateAnimation);
        iv_bigeye.startAnimation(loading_eyeRotateAnimation);
    }
}
